package com.ds.infra.rpc.example.provider.controller;

import com.ds.infra.rpc.example.api.ProductService;
import com.ds.infra.rpc.example.api.dto.ProductDTO;
import com.ds.infra.rpc.example.api.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/hi")
    public String hi() {
        return "Product::" + ThreadLocalRandom.current().nextInt(10000);
    }

    @GetMapping("/{id}")
    public ProductDTO getById(@PathVariable Long id) throws ApiException {
        return productService.getProductById(id);
    }

    @GetMapping
    public List<ProductDTO> getAll() throws ApiException {
        return productService.getAllProducts();
    }

    @PostMapping("/{id}/reduce-stock")
    public Boolean reduceStock(@PathVariable Long id,
                               @RequestParam Integer quantity) throws ApiException {
        return productService.reduceStock(id, quantity);
    }
}
